package com.yuanlai.quartz.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.yuanlai.quartz.model.ReturnResult;
import com.yuanlai.quartz.model.TtimedTask;
import com.yuanlai.quartz.model.enums.JobStatusEnum;
import com.yuanlai.quartz.model.enums.RequestStatusEnum;
import com.yuanlai.quartz.model.vo.TaskVo;
import com.yuanlai.quartz.service.ITaskService;
import com.yuanlai.quartz.utils.PageInfoUtil;

/**
 * 定时任务 管理页面处理器
 * @author yuanlai
 *
 */
@Controller
@RequestMapping("/taskManage")
public class TaskManageController {
	public final Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private ITaskService taskService;
	
	 @RequestMapping("/taskList")
	 @ResponseBody
	 public ReturnResult queryTask(TaskVo vo) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 TtimedTask record=new TtimedTask();
			 BeanUtils.copyProperties(vo, record);
			 //List<TaskVo> list = taskService.queryTask(record);
			 //returnResult.setData(list);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("查询任务数据出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("查询数据出错了");
		}

	     return returnResult;
	 }
	 @RequestMapping("/getdata")
	 @ResponseBody
	 public PageInfoUtil getdata(int limit ,int pageNum,String jobName,String jobGroup) {
			 TtimedTask record=new TtimedTask();
			 record.setJobName(StringUtils.isEmpty(jobName)?null:jobName);
			 record.setJobGroup(StringUtils.isEmpty(jobGroup)?null:jobGroup);
			PageInfoUtil page = taskService.queryTask(limit,pageNum,record);
	     return page;
	 }
	 @RequestMapping("/createTask")
	 @ResponseBody
	 public ReturnResult createTask(TaskVo vo) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 TtimedTask record=new TtimedTask();
			 BeanUtils.copyProperties(vo, record);
			 //taskService.addTask(record);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("新建的任务数据保存时出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("保存数据出错了");
		}

	  return 	returnResult;
	}
	 
	 @RequestMapping("/updateTask")
	 @ResponseBody
	 public ReturnResult updateTask(TaskVo vo) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 TtimedTask record=new TtimedTask();
			 BeanUtils.copyProperties(vo, record);
			 taskService.updateTask(record);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("保存修改后任务数据出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("修改后的数据保存出错了");
		}

	  return 	returnResult;
	}
	 
	 /**
	  * 修改cron表达式
	  * @param taskVo
	  * @return
	  */
	 @RequestMapping("/updateTaskCron")
	 @ResponseBody
	 public ReturnResult updateTaskCron(TaskVo vo) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			
			 taskService.updateTaskCron(vo);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("保存修改后任务数据出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("修改后的数据保存出错了");
		}

	  return 	returnResult;
	}
	 
	 @RequestMapping("/deleteTask")
	 @ResponseBody
	 public ReturnResult deleteTask(Long taskId) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 if (taskId!=null) {
				
				taskService.deleteTask(taskId);
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("删除任务数据时出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("删除数据时出错了");
		}

	    return 	returnResult;
	}
	 
	 @RequestMapping("/toUpdatePage")
	 public String toUpdatePage(Long taskId,Model model) {
		
		   TaskVo taskVo = taskService.queryOne(taskId);
		  model.addAttribute("task", taskVo);
		 return "task_modify";	 
	 }
	 @RequestMapping("/toUpdateCronPage")
	 public String toUpdateCronPage(Long taskId,Model model) {
		
		   TaskVo taskVo = taskService.queryOne(taskId);
		  model.addAttribute("task", taskVo);
		 return "task_modifyCron";	 
	 }

	 /**
	  * 开启任务状态
	  * @param id
	  * @return
	  */
	 @RequestMapping("/openTask")
	 @ResponseBody
	 public ReturnResult openTask(Long taskId) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 if (taskId!=null) {
				  taskService.openTask(taskId);
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("开启任务状态时出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("开启任务状态时出错了");
		}

	    return 	returnResult;
	}
	 
	 /**
	  * 关闭任务状态
	  * @param id
	  * @return
	  */
	 @RequestMapping("/closeTask")
	 @ResponseBody
	 public ReturnResult closeTask(Long taskId) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 if (taskId!=null) {
				 taskService.closeTask(taskId);
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("修改任务状态为关闭时出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("关闭任务出错了");
		}

	    return 	returnResult;
	}
	 /**
	  * 立即执行
	  * @param id
	  * @return
	  */
	 @RequestMapping("/runTask")
	 @ResponseBody
	 public ReturnResult runTask(Long taskId) {
		 ReturnResult returnResult=new ReturnResult();
		 returnResult.setStatus(RequestStatusEnum.SUCCESS);
		 try {
			 //立即运行任务
			 if (taskId!=null) {
				taskService.runTaskNow(taskId); 
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("立即启动任务出错",e);
			returnResult.setStatus(RequestStatusEnum.EXCEPTION);
			returnResult.setMessage("删除数据时出错了");
		}

	    return 	returnResult;
	}
}
